/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package pl.droidsonroids.gif;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;

/**
 * An {@link Image} which tries treating background and src as {@link GifDrawable}
 *-
 */

public class GifImageView extends Image implements Component.DrawTask {
    private static final String TAG_LOG = "GifImageView";

    private static final int DOMAIN_ID = 0xD000F00;

    private static final String LOG_FORMAT = "%{public}s: %{public}s";

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, TAG_LOG);

    private GifDrawable mGifDrawable;
    private GifDrawable mBackground;
    private boolean mFreezesAnimation;
    private AttrSet mAttrs;

    /**
     * A corresponding superclass constructor wrapper.
     *
     * @param context context
     * @see Image#Image(Context)
     */
    public GifImageView(Context context) {
        super(context);
        addDrawTask(this);
    }

    /**
     * Like equivalent from superclass but also try to interpret src and background
     * attributes as {@link GifDrawable}.
     *
     * @param context context
     * @param attrs attrs
     * @throws NotExistException NotExistException
     * @throws WrongTypeException WrongTypeException
     * @throws IOException IOException
     * * @see Image#Image (Context, AttrSet)
     */
    public GifImageView(Context context, AttrSet attrs) throws NotExistException, WrongTypeException, IOException {
        super(context, attrs);
        this.mAttrs = attrs;
        addDrawTask(this);

        /* postInit(GifViewUtils.initImageView(this, attrs, 0, 0));*/
    }

    /**
     * Like equivalent from superclass but also try to interpret src and background
     * attributes as GIFs.
     *
     * @param context context
     * @param attrs attrs
     * @param defStyle defStyle
     * @throws NotExistException NotExistException
     * @throws WrongTypeException WrongTypeException
     * @throws IOException IOException
     * @see Image#Image (Context, AttrSet, int)
     */
    public GifImageView(Context context, AttrSet attrs, String defStyle)
            throws NotExistException, WrongTypeException, IOException {
        super(context, attrs, defStyle);
        this.mAttrs = attrs;
        addDrawTask(this);
        /* postInit(GifViewUtils.initImageView(this, attrs, defStyle, 0));*/
    }

    /**
     * Like equivalent from superclass but also try to interpret src and background
     * attributes as GIFs.
     *
     * @param context context
     * @param attrs attrs
     * @param defStyle defStyle
     * @param defStyleRes defStyleRes
     * @throws NotExistException NotExistException
     * @throws WrongTypeException WrongTypeException
     * @throws IOException IOException
     *
     */

    public GifImageView(Context context, AttrSet attrs, String defStyle, int defStyleRes)
            throws NotExistException, WrongTypeException, IOException {
        super(context, attrs, defStyle);
        this.mAttrs = attrs;
        addDrawTask(this);

        /* postInit(GifViewUtils.initImageView(this, attrs, defStyle, defStyleRes)); */
    }

    private void postInit(GifViewUtils.GifImageViewAttributes result)
            throws NotExistException, WrongTypeException, IOException {
        mFreezesAnimation = result.freezesAnimation;
        if (result.mSourceResId > 0) {
            super.setImageElement(GifViewUtils.getElement(getContext(),result.mSourceResId));
        }
        if (result.mBackgroundResId > 0) {
            super.setBackground(GifViewUtils.getElement(getContext(), result.mBackgroundResId));
        }
    }

    /*@Override
    public void setImageURI(String uriStr) {
        if (!GifViewUtils.setGifImageUri(this, Uri.parse(uriStr))) {
            super.setImageURI(uriStr);
        }
    }*/

    @Override
    public void setPixelMap(int resId) {
        try {
            if (!GifViewUtils.setResource(this, true, resId)) {
                super.setPixelMap(resId);
            }
        } catch (IOException e) {
            HiLog.error(LABEL_LOG, LOG_FORMAT, TAG_LOG, "IoException");
        }
    }

    /**
     * setImageElement
     *
     * @param resId resId
     * @throws IOException IOException
     * @throws NotExistException NotExistException
     */
    public void setImageElement(int resId) throws IOException, NotExistException {
        if (!GifViewUtils.setResource(this, true, resId)) {
            super.setImageElement(GifViewUtils.getElement(getContext(), resId));
        }
    }

    /**
     * setImageElement
     *
     * @param element element
     */
    public void setImageElement(GifDrawable element) {
        mGifDrawable = element;
        super.setImageElement(mGifDrawable);
        mGifDrawable.setElementCallback(this);
    }

    /**
     * setBackgroundElement
     *
     * @param backgroundElement backgroundElement
     */
    public void setBackgroundElement(GifDrawable backgroundElement) {
        mBackground = backgroundElement;
        super.setBackground(mBackground);
        mBackground.setElementCallback(this);
    }

    /**
     * setBackgroundResource
     *
     * @param resId resId
     * @throws NotExistException NotExistException
     * @throws IOException IOException
     */
    public void setBackgroundResource(int resId) throws NotExistException, IOException {
        if (!GifViewUtils.setResource(this, false, resId)) {
            super.setBackground(GifViewUtils.getElement(getContext(), resId));
        }
    }

    /*@Override
    public Sequenceable onSaveInstanceState() {
        Element source = mFreezesAnimation ? getForegroundElement() : null;
        Element background = mFreezesAnimation ? getBackgroundElement() : null;
        return new GifViewSavedState(super.onSaveInstanceState(), source, background);
    }*/

    /*@Override
    public void onRestoreInstanceState(Sequenceable state) {
        if (!(state instanceof GifViewSavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        GifViewSavedState ss = (GifViewSavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        ss.restoreState(getForegroundElement(), 0);
        ss.restoreState(getBackgroundElement(), 1);
    }*/

    public void setFreezesAnimation(boolean freezesAnimation) {
        mFreezesAnimation = freezesAnimation;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mGifDrawable != null) {
            mGifDrawable.drawToCanvas(canvas);
        }
        if(mBackground != null) {
            mBackground.drawToCanvas(canvas);
        }
    }
}
